5.1 - Choose Your Own Adventure 2

What you'll be building

We told you this was just the beginning of our game empire! Now that you know more about JavaScript, you'll be able to create a much richer "choose your own adventure" game.

var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();
switch(troll) {
  case 'FIGHT':
    var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
    var smart = prompt("Are you smart?").toUpperCase();
    if(strong === 'YES' || smart === 'YES') {
      console.log("You only need one of the two! You beat the troll--nice work!");
    } else {
      console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
    }
    break;
  case 'PAY':
    var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase();
    var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
    if(money === 'YES' && dollars === 'YES') {
      console.log("Great! You pay the troll and continue on your merry way.");
    } else {
      console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
    }
    break;
  case 'RUN':
    var fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase();
    var headStart = prompt("Did you get a head start?").toUpperCase();
    if(fast === 'YES' || headStart === 'YES') {
      console.log("You got away--barely! You live to stroll through the forest another day.");
    } else {
      console.log("You're not fast and you didn't get a head start? You never had a chance! The troll eats you.");
    }
    break;
  default:
    console.log("I didn't understand your choice. Hit Run and try again, this time picking FIGHT, PAY, or RUN!");
}
Prompt

First, we'll need to use a promptstatement to ask our user what he or she wants to do. Recall that we use prompt like this:

var answer = prompt("Question to the user");
We store the result of using prompt in a variable so we can use the user's response to influence what the program does
var user = prompt("Are you ready to start?");
.toUpperCase() and .toLowerCase()

You may have noticed us use the.toUpperCase() function in the first exercise. We used it like this:

 var answer = prompt("Question to the user").toUpperCase();

This converted the user's answer to ALL CAPS before saving it in theanswer variable. This helps eliminate problems that might crop up if your program tests for 'YES' but your user typed in 'yes' or 'Yes'. The input becomes all caps before we test, so we only have to test for all caps!

You can also use .toLowerCase(), which converts a string to all lower-case letters.

Call either .toUpperCase() or.toLowerCase() on your prompt to ensure that the input you get from the user is capitalized the way you expect.
var user = prompt("Are you ready to start?").toUpperCase();
Switch

Great work! Now let's get to the heart of our game: the switch statement.

var user = prompt("Are you ready to start?").toUpperCase();
switch (user) {
    case 'YES':
        console.log("OK! Let's begin");
        break;
    case 'NO':
        console.log("Take your time, come back when you're ready");
        break;
    case 'MAYBE':
        console.log("You're so indecisive!!");
        break;
    default:
        console.log("Whateverrrr.....");
        
}
Logical operators

Good! Now let's spice things up with some logical operators.

Add some if/else statements to yourcases that check to see whether one condition and another condition are true, as well as whether one condition oranother condition are true. Use && and|| at least one time each.